#Created by Luxh on 2014/ 6 / 27

config = require('../config')
Schema = config.Schema
modeldb = config.modeldb
gridfs = new (require('../services/gridfs').FileStoreService)(config.fsdb.db)
gridfs_util = new (require('../services/gridfs_util').FileStoreUtil)(config.fsdb.db)
ObjectId = require('mongoose').Types.ObjectId
resErr = require('./util').resErr
fsdb = require('../config').fsdb.db
exec = require('child_process').exec
fs = require('fs')
async = require('async')
path = require('path')

genenrate_file_name = (suffix_name) ->
  now = new Date
  return now.getTime()+'.'+suffix_name

exports.common_upload = (req,res) ->
  file_field = req.param('file_field')
  if !file_field
    return resErr res,"文件上传对话框name属性不能为空"
  if !req.files[file_field]
    return resErr res,"文件不能为空"


  if !path.extname(req.files[file_field].name)||path.extname(req.files[file_field].name).toLowerCase()!='.pdf'||req.files[file_field].type != 'application/pdf'
    return resErr res,"只能上传pdf文档"

  req.files[file_field].name = req.param('file_rename') if req.param('file_rename')
  console.log "****file size begin****"
  console.log req.files[file_field].size
  console.log "****file size end****"
  if req.param("_max_file_size") &&  parseInt(req.param("_max_file_size")) < req.files[file_field].size
    limit = parseInt(req.param("_max_file_size"))
    mb = limit/(1000*1000)
    return resErr res,"文件大小不得超过" +mb+ "M"
  async.parallel({
    swf_id: (cb)->
      new_name = genenrate_file_name('swf')
      new_file = '/tmp/'+new_name
      cmd = 'pdf2swf -T 9 '+req.files[file_field].path+' -o '+new_file
      swf_id = new ObjectId()
      exec(cmd,(err,stdout,stderr)->
        if err
          cb('转换成pdf文件时出错',null)
        gridfs_util.uploadFile swf_id,new_file,new_name,'application/x-shockwave-flash', (err) ->
          cb(err,swf_id)
      )
    gridfs_id: (cb) ->
      gridfs_id = new ObjectId()
      gridfs.uploadFile gridfs_id,req.files[file_field], (err) ->
        cb(err,gridfs_id)

  },(err,results)->
    if err
      resErr res,err
    else
      result = {}
      result.success = true
      result.file_id = results.gridfs_id
      result.swf_id = results.swf_id
      result.file_name = req.files[file_field].name
      result.file_size = req.files[file_field].size
      res.json result
  )



exports.common_file_upload = (req,res)->
  file_field = req.param('file_field')
  if !file_field
    return resErr res,"文件上传对话框name属性不能为空"
  if !req.files[file_field]
    return resErr res,"文件不能为空"

  req.files[file_field].name = req.param('file_rename') if req.param('file_rename')
  console.log "****file size begin****"
  console.log req.files[file_field].size
  console.log "****file size end****"
  if req.param("_max_file_size") &&  parseInt(req.param("_max_file_size")) < req.files[file_field].size
    limit = parseInt(req.param("_max_file_size"))
    mb = limit/(1000*1000)
    return resErr res,"文件大小不得超过" +mb+ "M"

  gridfs_id = new ObjectId()
  gridfs.uploadFile gridfs_id,req.files[file_field], (err) ->
    if err
      resErr(res,err)
    else
      result = {}
      result.success = true
      result.file_id = gridfs_id
      result.file_name = req.files[file_field].name
      result.file_size = req.files[file_field].size
      res.json result





#上传otc产品相关的pdf文件，并转为swf文件
exports.upload_otc_file = (fileName,fileSize,contentType,file,callback) ->
  result = {}
  file.name = fileName
  async.parallel({
      swf_id: (cb)->
        new_name = genenrate_file_name('swf')
        new_file = '/tmp/'+new_name
        cmd = 'pdf2swf -T 9 '+file.path+' -o '+new_file
        swf_id = new ObjectId()
        exec(cmd,(err,stdout,stderr)->
          if err
            cb('转换成swf文件时出错',null)
          gridfs_util.uploadFile swf_id,new_file,new_name,'application/x-shockwave-flash', (err) ->
            cb(err,swf_id)
        )
      gridfs_id: (cb) ->
        gridfs_id = new ObjectId()
        console.log '系统公告----1--'+file.type
        console.log '系统公告----2--'+file.name
        gridfs.uploadFile gridfs_id,file, (err) ->
          cb(err,gridfs_id)

    },(err,results)->
        if err
          callback(err)
        else
          result.success = true
          result.file_id = results.gridfs_id
          result.swf_id = results.swf_id
          result.file_name = fileName
          result.file_size = fileSize
          callback(null,result)
  )


#OTC 上传
exports.otc_upload = (fileName,fileSize,contentType,file,cb) ->
  result = {}
  if !file
    cb('文件不能为空！',null)
  else
    #10MB
    max_value = 10240000
    if fileSize > max_value
      cb('文件大小不能超过10MB！',null)
    else
      file.name = fileName
      gridfs_id = new ObjectId()
      gridfs.uploadFile gridfs_id,file, (err) ->
        if err
          cb(err,null)
        else
          result.success = true
          result.file_id = gridfs_id
          result.file_name = fileName
          result.file_size = fileSize
          cb(null,result)


exports.otc_upload_pdf_and_swf = (pdfFileName,pdf,swf,cb) ->
  result = {}
  if !pdf || !swf
    cb('文件不能为空！',null)
  else
    #10MB
    max_value = 10240000
    if pdf.size > max_value || swf.size > max_value
      cb('文件大小不能超过10MB！',null)
    else
      async.parallel({
          pdf_id: (cbf)->
            gridfs_id = new ObjectId()
            pdf.name = pdfFileName
            gridfs.uploadFile gridfs_id,pdf, (err) ->
              if err
                cbf(err,null)
              else
                cbf(null,gridfs_id)

          swf_id: (cbf) ->
            gridfs_id = new ObjectId()
            swf.type = 'application/x-shockwave-flash'
            gridfs.uploadFile gridfs_id,swf, (err) ->
              if err
                cbf(err,null)
              else
                cbf(null,gridfs_id)
        },(err,results)->
          if err
            cb(err)
          else
            result.pdf_id = results.pdf_id
            result.swf_id = results.swf_id
            cb(null,result)
      )

